package byj.com.meishi;

import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import byj.com.meishi.bean.Person;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2016/9/21 0021.
 */
public class RegisterActivity extends AppCompatActivity{

    private EditText et_phone;
    private EditText password;
    private EditText yanPassword;
    private EditText yanzheng;
    private Button sendYan;
    //    private TextWatcher textWathcer =new TextWatcher() {
//        @Override
//        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//        }
//
//        @Override
//        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//        }
//
//        @Override
//        public void afterTextChanged(Editable editable) {
//            String mima= password.getText().toString().trim();
//            String yanmima = yanPassword.getText().toString().trim();
//            if(mima.equals(yanmima)){
//                Toast.makeText(RegisterActivity.this,"密码验证成功！！！",Toast.LENGTH_SHORT).show();
//            }else{
//                password.setText("");
//                yanPassword.setText("");
//                Toast.makeText(RegisterActivity.this,"两次输入密码不一致，请重新输入",Toast.LENGTH_SHORT).show();
//            }
//
//        }
//    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.regeister_layout);
        initView();
//        yanPassword.addTextChangedListener(textWathcer);




    }

//    private void yanPassword() {
//
//        while (true){
//
//        }
//
//    }

    private void initView() {
        et_phone = (EditText) findViewById(R.id.phonenumber);
        password = (EditText) findViewById(R.id.password);
        yanPassword = (EditText) findViewById(R.id.yan_password);
        yanzheng = (EditText) findViewById(R.id.et_yanzhengma);
        sendYan = (Button) findViewById(R.id.btn_yanzheng);
    }

    public void getYan(final View view) {

        String phonenumber = et_phone.getText().toString().trim();
        if(phonenumber.length()!=11){
            Toast.makeText(this, "请输入11位有效手机号码", Toast.LENGTH_SHORT).show();
        }else{
            BmobSMS.requestSMSCode(phonenumber, "验证码", new QueryListener<Integer>() {
                @Override
                public void done(Integer integer, BmobException e) {
                    if (e == null) {
                        //发送成功时，让获取验证码按钮不可点击，且为灰色
                        sendYan.setClickable(false);
                        sendYan.setBackgroundColor(Color.GRAY);
                        Toast.makeText(RegisterActivity.this, "验证码发送成功，请尽快使用", Toast.LENGTH_SHORT).show();
                        /**
                         * 倒计时1分钟操作
                         * 说明：
                         * new CountDownTimer(60000, 1000),第一个参数为倒计时总时间，第二个参数为倒计时的间隔时间
                         * 单位都为ms，其中必须要实现onTick()和onFinish()两个方法，onTick()方法为当倒计时在进行中时，
                         * 所做的操作，它的参数millisUntilFinished为距离倒计时结束时的时间，以此题为例，总倒计时长
                         * 为60000ms,倒计时的间隔时间为1000ms，然后59000ms、58000ms、57000ms...该方法的参数
                         * millisUntilFinished就等于这些每秒变化的数，然后除以1000，把单位变成秒，显示在textView
                         * 或Button上，则实现倒计时的效果，onFinish()方法为倒计时结束时要做的操作，此题可以很好的
                         * 说明该方法的用法，最后要注意的是当new CountDownTimer(60000, 1000)之后，一定要调用start()
                         * 方法把该倒计时操作启动起来，不调用start()方法的话，是不会进行倒计时操作的
                         */
                        new CountDownTimer(60000, 1000) {
                            @Override
                            public void onTick(long millisUntilFinished) {
//                                sendYan.setBackgroundResource(R.drawable.button_shape02);
                                sendYan.setText(millisUntilFinished / 1000 + "秒");
                            }

                            @Override
                            public void onFinish() {
                                sendYan.setClickable(true);
//                                sendYan.setBackgroundResource(R.drawable.button_shape);
                                sendYan.setText("重新发送");
                            }
                        }.start();
                        Log.e("MESSAGE:", "4");
                    }
                    else {
                        Toast.makeText(RegisterActivity.this, "验证码发送失败，请检查网络连接", Toast.LENGTH_SHORT).show();
                    }

                }
            });

        }


    }

    public void register(View view) {
        final String name=et_phone.getText().toString().trim();
        final String mima = password.getText().toString().trim();
        String yanmima=yanPassword.getText().toString().trim();
        if(!mima.equals(yanmima)){
            password.setText("");
            yanPassword.setText("");
            Toast.makeText(RegisterActivity.this,"两次输入密码不一致，请重新输入",Toast.LENGTH_SHORT).show();
        }else{
            BmobSMS.verifySmsCode(name, yanzheng.getText().toString(), new UpdateListener() {
                @Override
                public void done(BmobException e) {
                    if(e==null){
                        //查找Person表里面id为6b6c11c537的数据
                        BmobQuery<Person> bmobQuery = new BmobQuery<Person>();
                        bmobQuery.getObject("" + name, new QueryListener<Person>() {
                            @Override
                            public void done(Person person, BmobException e) {
                                if(e==null){
                                    Toast.makeText(RegisterActivity.this,"用户名已存在",Toast.LENGTH_SHORT).show();
                                    et_phone.setText("");
                                    password.setText("");
                                    yanPassword.setText("");

                                }else {
                                    Person p=new Person();
                                    p.setName(name);
                                    p.setPassword(mima);
                                    p.save(new SaveListener<String>() {
                                        @Override
                                        public void done(String s, BmobException e) {
                                            if(e==null){
                                                Toast.makeText(RegisterActivity.this,"添加数据成功，返回objectId为："+name,Toast.LENGTH_SHORT).show();
                                                finish();
                                            }else {
                                                Toast.makeText(RegisterActivity.this,"创建数据失败：" + e.getMessage(),Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    } else{
                        Toast.makeText(RegisterActivity.this,"验证码输入有误",Toast.LENGTH_SHORT).show();
                    }
                }
            });

        }
    }
}
